/*
Copyright (C) 2011  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package WordTable;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterJob;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;

import GuiTools.*;
import Settings.*;
import Storage.*;
import WordTable.WCSettings;
import WordTable.WordTable.WTMenu;

/**
 * @author Christoph Schnack
 *
 */
class PrintPresettings{
	TribuitFrame fontFrame;
	JSpinner tsCount,msCount;
	JCheckBox verticalSeparatorCB, eSentenceCB, noteCB, phonCB, metaCB, synonymsCB,wordsBoldCB, termClassCB;
	Storage MAINVOC;
	Settings SET;
	Language LANG;
	WTMenu sourceMenu;
	Vector<Word> words;
	String vocName;
	WCSettings wcSettings;
	int ts=12,ms=10;
	
	public PrintPresettings(Storage mainvocArg, Settings setArg, WTMenu menuArg, WCSettings wcSettingsArg){
		MAINVOC = mainvocArg;
		SET = setArg;
		LANG = SET.getLanguage();
		sourceMenu = menuArg;
		wcSettings = wcSettingsArg;
		words = wcSettings.getWords();
		vocName = wcSettings.getVocName();
		fontFrame = new TribuitFrame(LANG.getPrint());
		Header fontHeader = new Header(LANG.getPrintSettings(),Color.white,GuiTools.BGCOLOR,25,new Font(Font.SERIF,Font.BOLD,16));
		SpinnerModel model = new SpinnerNumberModel(ts,1,30,1); 
		tsCount = new JSpinner(model);
		tsCount.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				Integer tmp = (Integer)tsCount.getValue();
				ts=tmp.intValue();
			}
		});
		SpinnerModel model2 = new SpinnerNumberModel(ms,1,30,1); 
		msCount = new JSpinner(model2);
		msCount.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				Integer tmp = (Integer)msCount.getValue();
				ms=tmp.intValue();
			}
		});
		
		
		JButton previewBtn = new JButton(LANG.getPreview());
		previewBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//WCSettings wcs = new WCSettings(wcSettings.getWords(), wcSettings.getVocNameA(), wcSettings.getVocNameB(), wcSettings.getTestLang2(), metaCB.isSelected(), phonCB.isSelected(), noteCB.isSelected(), eSentenceCB.isSelected(), synonymsCB.isSelected());
				//PrintSettings ps = new PrintSettings(verticalSeparatorCB.isSelected(),wordsBoldCB.isSelected(),termClassCB.isSelected());
				//PrintPreview pp = new PrintPreview(LANG,new Print(MAINVOC,words,wcs,ps,ts,ms));
			}
		});
		
		JButton okBtn = new JButton(LANG.getAccept());
		okBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				WCSettings wcs = new WCSettings(wcSettings.getWords(), wcSettings.getVocNameA(), wcSettings.getVocNameB());
				wcs.setTestLang2Arg(wcSettings.getTestLang2());
				wcs.setTestMetasArg(metaCB.isSelected());
				wcs.setShowPhoneticsArg(phonCB.isSelected());
				wcs.setShowNoteArg(noteCB.isSelected());
				wcs.setShowESentenceArg(eSentenceCB.isSelected());
				wcs.setShowSynonymsArg(synonymsCB.isSelected());
				PrintSettings ps = new PrintSettings(verticalSeparatorCB.isSelected(),wordsBoldCB.isSelected(), termClassCB.isSelected());
				Print p = new Print(MAINVOC,words,wcs,ps,ts,ms);
				PrinterJob pj = PrinterJob.getPrinterJob();
				pj.setPrintable(p);
				pj.printDialog();
				try{
					fontFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
					pj.print();
					fontFrame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
				}
				catch(Exception ex){
					ex.printStackTrace();
				}
				sourceMenu.setPSDRunning(false);
				fontFrame.dispose();
			}
		});
		JButton cancelBtn = new JButton(LANG.getCancel());
		cancelBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sourceMenu.setPSDRunning(false);
				fontFrame.dispose();
			}
		});
		FlowLayout fl = new FlowLayout();
		fl.setHgap(30);
		JPanel spinnerPanel = new JPanel();
		spinnerPanel.setLayout(fl);
		spinnerPanel.add(new LabelPanel(LANG.getWords()+":",tsCount));
		spinnerPanel.add(new LabelPanel(LANG.getMetas()+":",msCount));
		spinnerPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), LANG.getFontSize()+":"));
		
		verticalSeparatorCB = new JCheckBox(LANG.getVerticalSeparatorLine(),false);
		wordsBoldCB = new JCheckBox(LANG.getWordsBold(),true);
		JPanel optionPanel = new JPanel();
		optionPanel.setLayout(new BoxLayout(optionPanel,BoxLayout.Y_AXIS));
		optionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),LANG.getOptions()+":"));
		optionPanel.add(verticalSeparatorCB);
		optionPanel.add(wordsBoldCB);
		
		phonCB = new JCheckBox(LANG.getPhonetic(),wcSettings.getShowPhonetics());
		noteCB = new JCheckBox(LANG.getNote(),wcSettings.getShowNote());
		metaCB = new JCheckBox(LANG.getMetainformation(),wcSettings.getTestMetas());
		synonymsCB = new JCheckBox(LANG.getSynonyms(),wcSettings.getShowSynonyms());
		eSentenceCB = new JCheckBox(LANG.getExampleSentence(),wcSettings.getShowESentence());
		termClassCB = new JCheckBox(LANG.getTermClass(),true);
		
		JPanel optionPanel2 = new JPanel();
		optionPanel2.setLayout(new BoxLayout(optionPanel2,BoxLayout.Y_AXIS));
		optionPanel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), (LANG.getShow()+":")));
		optionPanel2.add(phonCB);
		optionPanel2.add(metaCB);
		optionPanel2.add(synonymsCB);
		optionPanel2.add(noteCB);
		optionPanel2.add(eSentenceCB);
		optionPanel2.add(termClassCB);
		
		JPanel centerPanel = new JPanel(new GridBagLayout());
		centerPanel.setBackground(Color.gray);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10,10,0,10);
		gbc.gridy = 1;
		centerPanel.add(spinnerPanel,gbc);
		gbc.insets = new Insets(10,10,10,10);
		gbc.gridy = 2;
		centerPanel.add(optionPanel,gbc);
		gbc.insets = new Insets(0,10,10,10);
		gbc.gridy = 3;
		centerPanel.add(optionPanel2,gbc);
		
		JPanel btnPanel = new JPanel();
		btnPanel.setBackground(GuiTools.BGCOLOR);
		btnPanel.add(okBtn);
		btnPanel.add(previewBtn);
		btnPanel.add(cancelBtn);
		fontFrame.getContentPane().add(BorderLayout.NORTH,fontHeader);
		fontFrame.getContentPane().add(BorderLayout.CENTER,centerPanel);
		fontFrame.getContentPane().add(BorderLayout.SOUTH,btnPanel);
		fontFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				sourceMenu.setPSDRunning(false);
			}
		});
		fontFrame.showUp(100, 100);
	}
}

